/*
 * MainView.java
 *
 * Created on Dec 23, 2010, 5:26:33 PM
 */

package view;

import Eventlisteners.LogEvent;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import model.Maid;

public class MainView extends javax.swing.JFrame{
    Maid jenkins;
    LogEvent logEvent;

    /** Creates new form MainView */
    public MainView(Maid themaid, LogEvent dispatch) {
        jenkins = themaid;
        logEvent = dispatch;
        initComponents();

        jenkins.setTraversal(traversalCheckbox.isSelected());
        jenkins.setMoveToFolder(moveToFolderCheckbox.isSelected());
        this.show();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        traversalCheckbox = new javax.swing.JCheckBox();
        moveToFolderCheckbox = new javax.swing.JCheckBox();
        cleanBtn = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        fileExtTextbox = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        rootDirectoryTextbox = new javax.swing.JTextField();
        changeRootDirectoryBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        folderNameTextbox = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("The Maid");

        traversalCheckbox.setText("Traverse Subdirectories");
        traversalCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                traversalCheckboxActionPerformed(evt);
            }
        });

        moveToFolderCheckbox.setSelected(true);
        moveToFolderCheckbox.setText("Move files to folder");
        moveToFolderCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveToFolderCheckboxActionPerformed(evt);
            }
        });

        cleanBtn.setText("Clean");
        cleanBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleanBtnActionPerformed(evt);
            }
        });

        jLabel2.setText("File Extentions to delete (comma separated)");

        fileExtTextbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileExtTextboxActionPerformed(evt);
            }
        });

        jLabel3.setText("Root Directory");

        rootDirectoryTextbox.setEnabled(false);
        rootDirectoryTextbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rootDirectoryTextboxActionPerformed(evt);
            }
        });

        changeRootDirectoryBtn.setText("Change");
        changeRootDirectoryBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeRootDirectoryBtnActionPerformed(evt);
            }
        });

        jLabel1.setText("Folder name:");

        folderNameTextbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                folderNameTextboxActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(traversalCheckbox)
                    .add(layout.createSequentialGroup()
                        .add(moveToFolderCheckbox)
                        .add(18, 18, 18)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(folderNameTextbox, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                    .add(cleanBtn)
                    .add(jLabel3)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, rootDirectoryTextbox)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, fileExtTextbox)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(changeRootDirectoryBtn)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel3)
                .add(2, 2, 2)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(rootDirectoryTextbox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(changeRootDirectoryBtn))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .add(2, 2, 2)
                .add(fileExtTextbox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(traversalCheckbox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(moveToFolderCheckbox)
                    .add(jLabel1)
                    .add(folderNameTextbox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(cleanBtn))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void traversalCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_traversalCheckboxActionPerformed
        jenkins.setTraversal(traversalCheckbox.isSelected());
    }//GEN-LAST:event_traversalCheckboxActionPerformed

    private void fileExtTextboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileExtTextboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fileExtTextboxActionPerformed

    private void rootDirectoryTextboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rootDirectoryTextboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rootDirectoryTextboxActionPerformed

    private void changeRootDirectoryBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeRootDirectoryBtnActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int jfcReturn = jfc.showOpenDialog(null);
        if(jfcReturn == JFileChooser.APPROVE_OPTION){
            jenkins.setStartingPath(jfc.getSelectedFile());
            rootDirectoryTextbox.setText(jfc.getSelectedFile().toString());
        }
    }//GEN-LAST:event_changeRootDirectoryBtnActionPerformed

    private void cleanBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanBtnActionPerformed
        String fileExtentions = fileExtTextbox.getText();
        fileExtentions = fileExtentions.toLowerCase();
        String[] extArray = fileExtentions.split(",");

        if(!folderNameTextbox.getText().isEmpty())
            jenkins.changeFolderName(folderNameTextbox.getText());
        jenkins.addDustBunnies(extArray);
        jenkins.clean();
    }//GEN-LAST:event_cleanBtnActionPerformed

    private void moveToFolderCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveToFolderCheckboxActionPerformed
        jenkins.setMoveToFolder(moveToFolderCheckbox.isSelected());
        if(!moveToFolderCheckbox.isSelected()){

            JOptionPane.showMessageDialog(this, "Warning: This will permanently delete your files.");
        }
    }//GEN-LAST:event_moveToFolderCheckboxActionPerformed

    private void folderNameTextboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_folderNameTextboxActionPerformed

    }//GEN-LAST:event_folderNameTextboxActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton changeRootDirectoryBtn;
    private javax.swing.JButton cleanBtn;
    private javax.swing.JTextField fileExtTextbox;
    private javax.swing.JTextField folderNameTextbox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JCheckBox moveToFolderCheckbox;
    private javax.swing.JTextField rootDirectoryTextbox;
    private javax.swing.JCheckBox traversalCheckbox;
    // End of variables declaration//GEN-END:variables
}
